package org.jeecg.modules.tongxjl.controller;

import java.util.*;
import java.util.stream.Stream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.poi.ss.formula.functions.T;
import org.checkerframework.checker.units.qual.A;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.tongxjl.entity.AgTongxjl;
import org.jeecg.modules.tongxjl.service.IAgTongxjlService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 通行记录
 * @Author: jeecg-boot
 * @Date: 2020-09-24
 * @Version: V1.0
 */
@Api(tags = "通行记录")
@RestController
@RequestMapping("/tongxjl/agTongxjl")
@Slf4j
public class AgTongxjlController extends JeecgController<AgTongxjl, IAgTongxjlService> {
    @Autowired
    private IAgTongxjlService agTongxjlService;

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedisUtil redisUtil;

    @GetMapping(value = "/list4zhul")
    public Result<?> list4zhul(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {


//        List <AgTongxjl> zhula= agTongxjlService.list(new QueryWrapper<AgTongxjl>().eq("shebmc", "主楼").last("limit 0,10"));
//        LinkedList<AgTongxjl> zhul = new LinkedList <>();
//        for (int i = 0; i < zhula.size(); i++) {
//            zhul.add(zhul.size(),zhula.get(i));
//            if (zhul.size()>5){
//                zhul.pop();
//            }
//        }
//            redisUtil.set("主楼通行记录",zhul );


        Object zhul = redisUtil.get("主楼通行记录");
        IPage<Object> pageList=new Page<Object>(pageNo, pageSize);
        pageList.setRecords((List) redisUtil.get("主楼通行记录"));

        log.info("redis主楼通行记录:"+pageList);


        return Result.ok(pageList);
    }

    @GetMapping(value = "/list4dam")
    public Result<?> list4dam(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {

//        Page<AgTongxjl> page = new Page<AgTongxjl>(pageNo, pageSize);
//        IPage<AgTongxjl> pageList = agTongxjlService.page(page, new LambdaQueryWrapper<AgTongxjl>()
//                .likeRight(AgTongxjl::getShebmc, "大门")
//                .or()
//                .likeRight(AgTongxjl::getShebmc, "中心校区1号门")
//                .orderByDesc(AgTongxjl::getTongxsj));


        Object dam = redisUtil.get("大门通行记录");
        IPage<Object> pageList=new Page<Object>(pageNo, pageSize);
        pageList.setRecords((List) redisUtil.get("大门通行记录"));


        log.info("redis大门通行记录:"+pageList);
        return Result.ok(pageList);
    }

    /**
     * 分页列表查询
     *
     * @param agTongxjl
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "通行记录-分页列表查询")
    @ApiOperation(value = "通行记录-分页列表查询", notes = "通行记录-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(AgTongxjl agTongxjl,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<AgTongxjl> queryWrapper = QueryGenerator.initQueryWrapper(agTongxjl, req.getParameterMap());
        Page<AgTongxjl> page = new Page<AgTongxjl>(pageNo, pageSize);
        IPage<AgTongxjl> pageList = agTongxjlService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param agTongxjl
     * @return
     */
    @AutoLog(value = "通行记录-添加")
    @ApiOperation(value = "通行记录-添加", notes = "通行记录-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody AgTongxjl agTongxjl) {
        agTongxjlService.save(agTongxjl);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param agTongxjl
     * @return
     */
    @AutoLog(value = "通行记录-编辑")
    @ApiOperation(value = "通行记录-编辑", notes = "通行记录-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody AgTongxjl agTongxjl) {
        agTongxjlService.updateById(agTongxjl);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "通行记录-通过id删除")
    @ApiOperation(value = "通行记录-通过id删除", notes = "通行记录-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        agTongxjlService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "通行记录-批量删除")
    @ApiOperation(value = "通行记录-批量删除", notes = "通行记录-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.agTongxjlService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "通行记录-通过id查询")
    @ApiOperation(value = "通行记录-通过id查询", notes = "通行记录-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        AgTongxjl agTongxjl = agTongxjlService.getById(id);
        if (agTongxjl == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(agTongxjl);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param agTongxjl
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AgTongxjl agTongxjl) {
        return super.exportXls(request, agTongxjl, AgTongxjl.class, "通行记录");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AgTongxjl.class);
    }

}
